--                local this_pos = thisActor:pos()--cc.pAdd(thisActor:pos(), thisActor.shift)
--                local other_pos = cc.pAdd(_actor:pos(), _actor.shift)
--                local deltaX, deltaY = this_pos.x - other_pos.x, this_pos.y - other_pos.y
--                local delta = math.sqrt(deltaX * deltaX + deltaY * deltaY)
--                local R = thisActor:radius() + _actor:radius()

--                if thisActor.player == _actor.player then          -- 己方行动预测绕行
--                    local caseNum = 25  -- 计算下10帧的点的坐标进行预测判定
--                    local this_pos = thisActor:pos()
--                    local this_shift = {x=thisActor.shift.x * caseNum,y=thisActor.shift.y * caseNum}
--                    this_pos = cc.pAdd(this_pos,this_shift)
--                    local deltaX, deltaY = this_pos.x - other_pos.x, this_pos.y - other_pos.y
--                    local delta = math.sqrt(deltaX * deltaX + deltaY * deltaY)
--                    --只做最近的绕行
--                    if delta <= R  then
--                        if otherActor.round == nil or R - delta > otherActor.round then

--                        otherActor.roundactor = _actor
--                        otherActor.round = R - delta                         

--                        local c_pos = td.PointSlopeForm({a_pos = this_pos ,b_pos = other_pos,a_k = this_shift})
----                        dump(this_pos)  dump(other_pos)  dump(c_pos)       
--                        local roundLength = cc.pGetDistance(other_pos,c_pos) 

--                        if roundLength <= 0.1 * R then  --直指圆心的时候
----                            print(thisActor.id.."我绕开")
--                            roundLength = 0.1 * R
--                            local roundDelta = (math.abs(thisActor.id - _actor.id)%2-0.5)*2
--                            local pNormalize = cc.pNormalize(this_pos,c_pos)
--                            otherActor.roundX,otherActor.roundY = -pNormalize.x*roundDelta,pNormalize.y*roundDelta
--                        else    --有偏差时
----                            print(thisActor.id.."我绕开中")
--                            local pNormalize = cc.pNormalize(cc.pSub(c_pos,other_pos))
--                            otherActor.roundX = pNormalize.x
--                            otherActor.roundY = pNormalize.y
--                        end

--                            otherActor.roundcoefficient = (R-roundLength)/R --* caseNum --cc.pGetLength(this_shift)                        -- 基于距离的绕行推挤系数
----                            dump(otherActor.roundcoefficient) dump(otherActor.roundX)  dump(otherActor.roundY)
--                        end
--                    end
--                end
--                if delta < R then
--                    -- 距离判定
--                    if otherActor.range == nil or R - delta > otherActor.range then
--                        -- 每一次只做最近的推挤
--                        --                                math.random(0,1)    --下面带有actor.id%的均为附带基于ID的随机位移
--                        otherActor.actor = _actor
--                        otherActor.delta = delta
--                        otherActor.range = R - delta
--                        otherActor.deltaX = deltaX / delta
--                        otherActor.deltaY = deltaY / delta

--                        if delta ==0 then
--                            -- 贴身根据ID排挤

--                            local deltaId = thisActor.id - _actor.id
--                            if math.abs(deltaId) % 2 == 1 then
--                                otherActor.deltaX = 0.7
--                            else
--                                otherActor.deltaX = -0.7
--                            end

--                            if deltaId > 0 then
--                                otherActor.deltaY = 0.7
--                            else
--                                otherActor.deltaY = -0.7
--                            end
--                        end

--                        -- 基于质量 的碰撞推挤系数
--                        if thisActor:weight() >= _actor:weight() * 2 then
--                            otherActor.coefficient = 0
--                        elseif thisActor:weight() <= _actor:weight() / 2 then
--                            otherActor.coefficient = 1
--                        else
--                            otherActor.coefficient = 0.5--_actor:weight() /(_actor:weight() + thisActor:weight())                            -- 体重系数
--                        end
--                        otherActor.coefficient = otherActor.coefficient * otherActor.range*1                       --  乘以距离
--                    end                    -- 每一次只做最近的推挤
--                    --                        if otherActor.range > 1 then  table.insert(thisActor._repulsion,{ x = otherActor.coefficient*otherActor.deltaX , y = otherActor.coefficient*otherActor.deltaY })  end
--                    --                        break -- 获取第一个碰撞到的的actor
--                end



--    local _shiftPos = self.shift
    if self.repul then 
--        self:MoveTo(cc.pAdd(cc.pAdd(self:pos(),self.shift),self.repul))
        local UnitVector = cc.pNormalize(cc.pAdd(self.shift,self.repul))
--        self.shift = cc.pMul(UnitVector,self._property.move_speed*dt)
        self.shift = cc.pAdd(self.shift,self.repul)
--        self:MoveTo(cc.pAdd(cc.pAdd(self:pos(),self.shift),self.repul))
        self.repul = nil 
    end
--    else
    if self.round then 
        self:MoveTo(cc.pAdd(cc.pAdd(self:pos(),self.shift),self.round))
--        self.shift = cc.pAdd(self.shift,self.round)
        self.round = nil
    else
        self:MoveTo(cc.pAdd(self:pos(),self.shift))
    end
--    self:MoveTo(cc.pAdd(self:pos(),self.shift))


--        if otherActor.actor then --and otherActor.range > 0 then   --每一次只做最近的推挤
----            print("actor:" .. thisActor.id .. " 和 actor:" .. otherActor.actor.id .. "发生了碰撞\n碰撞系数为：" .. otherActor.coefficient .. "大小为：" .. otherActor.range .. "像素 受到的力的方向为 X:" .. otherActor.deltaX .. " Y:" .. otherActor.deltaY)
----            print("碰撞")
--            local repul = { x = otherActor.coefficient * otherActor.deltaX, y = otherActor.coefficient * otherActor.deltaY }
--            thisActor:SetRepul(repul)
--        end
--        elseif otherActor.roundactor then
----            print("绕行")
--            local round = { x = otherActor.roundcoefficient * otherActor.roundX, y = otherActor.roundcoefficient * otherActor.roundY }
--            thisActor:SetRepul(round)
--        end

--function ActorBase:set_attack(data)
--    for tag, var in pairs(self._property) do
--        if data[tag] then self._property[tag] = data[tag] end
--    end
--end

--function ActorBase:set_health(data)
--    for tag, var in pairs(self._property) do
--        if data[tag] then self._property[tag] = data[tag] end
--    end
--end



--    if data._attack then self:set_attack(data._attack) end
--    if data._health then self:set_health(data._health) end
--    if data._pos then self._pos = data._pos end
--    if data._target  then self._target = data._target end
--    if data.player then self._player = data.player end

function ActorBase:Update2(dt)
    --  一：如果有指定攻击敌人则：
    --      ①如果指定攻击敌人在射程内,则直接攻击。
    --      ②指定攻击敌人不再射程内,则删除指定攻击敌人id 
    --  二：否则,如果有指定仇恨敌人则：
    --      〇在不超过指定仇恨敌人的距离内进行判定：如果有更近的敌人则将其设定为新的指定仇恨敌人
    --      ①如果指定仇恨敌人在攻击距离内,则将其指定为指定攻击敌人,并删除指定仇恨敌人id
    --      ②如果指定仇恨敌人不在攻击距离内,则向指定仇恨敌人前进
    --  三：如果没有指定攻击和仇恨的敌人则：
    --      〇在仇恨范围内搜索敌人,并将搜索到的最近的敌人设置为指定仇恨敌人
    --      ①如果还没有目标敌人则往下一个目标位置前进
    --      ②抵达目标位置后删除当前目标位置,向下一个目标位置前进
    
--    dump(dt)
--    local speed = self._property.move_speed*dt

    local _pos = td.GetGridForPos(self:pos())
    local hatredForPos = td.SeekRoadFor4(_pos,self._property.hatred)

    local wantMove 
    
--    if self.id == 18 then 
--         print("我是："..self.id)
--    end
--        print("我是："..self.id..
--    " 攻击目标为："..(self:enemyAttack()==nil and "无" or self:enemyAttack().id  )
--    .." 仇恨目标为："..(self:enemyHatred()==nil and "无" or self:enemyHatred().id  ) )


    --思维（寻路，移动，攻击）

    if self:enemyAttack() and self:enemyAttack().isLive then
--        wantMove = _pos
--        print("当前攻击目标为："..self:enemyAttack().id)
        local enemy_pos = self:enemyAttack():pos()
        local enemy_range =  ( _pos.x - enemy_pos.x ) * ( _pos.x - enemy_pos.x ) + ( _pos.y - enemy_pos.y ) * ( _pos.y - enemy_pos.y ) 
        local attackRange = self._property.range+self:radius() + self:enemyAttack():radius()
        if enemy_range <= attackRange*attackRange then --enemyAttack的敌人在射程内，则攻击
--            print("我在使劲砍目标："..self:enemyAttack().id)
        else --否则 删除 enemyAttack
            self:enemyAttack(false)
--          print("我砍不到目标") 
        end 
    elseif self:enemyHatred() and self:enemyHatred().isLive then 
--        print("当前仇恨目标为："..self:enemyHatred().id)
        local enemy_pos = self:enemyHatred():pos()
        local enemy_range = cc.pDistanceSQ(_pos,enemy_pos)--( _pos.x - enemy_pos.x ) * ( _pos.x - enemy_pos.x ) + ( _pos.y - enemy_pos.y ) * ( _pos.y - enemy_pos.y )
        local attackRange = self._property.range+self:radius() + self:enemyHatred():radius()
        if enemy_range <= attackRange*attackRange then --enemyHatred的敌人在射程内，则攻击
--            print("目标进入攻击范围，攻击")
            self:enemyAttack(self:enemyHatred())
            self:enemyHatred(false)
        elseif enemy_range > self:hatred()*self:hatred() then   --enemyHatred的敌人在仇恨范围外，则删除 enemyHatred   
--            print("目标脱离仇恨范围，继续前进")
            self:enemyHatred(false)
        else  --否则 判定是否还是最近的 enemyHatred ，如果有更近的则转换成别的enemyHatred
            local _enemyHatred = td.LookForNearestEnemy(_pos,self:hatred(),hatredForPos,self._player)
            if _enemyHatred then 
--                print("有了新欢:".._enemyHatred.id)
                self:enemyHatred(_enemyHatred)
            else
--                print("还是旧爱:"..self:enemyHatred().id)
            end
--            print("继续向目标移动")
            wantMove = self:enemyHatred():pos()   --并向其移动
        end
    elseif #self._target.pos ~= 0 then 
--        print("正在前往目标地点：X:"..self._target.pos[1].x.." Y:"..self._target.pos[1].y)        
        local _enemyHatred = td.LookForNearestEnemy(_pos,self:hatred(),hatredForPos,self._player)     --给定指定格子范围，在范围内搜索最近的敌人,当前位置，仇恨范围，需要搜索的格子，当前玩家，搜索自己or敌人，
        if _enemyHatred then --如果仇恨范围内有敌人则添加到enemyHatred
--            print("在前往目标点时发现敌人：".._enemyHatred.id) 
            self:enemyHatred(_enemyHatred)
            wantMove = _enemyHatred:pos()            
        else    --如果否则向着目标点前进
            wantMove = self._target.pos[1] 
        end 

        if cc.pDistanceSQ(self._target.pos[1],_pos) <= self:radius()*self:radius() then 
--            print("到达目的地")
            table.remove(self._target.pos,1) 
        end
    else --如果没有目标点则原地不动并且搜索敌人
--        print("成为了咸鱼")
        self:enemyHatred(td.LookForNearestEnemy(_pos,self:hatred(),hatredForPos,self._player) or false)
        if self:enemyHatred() then --如果仇恨范围内有敌人则添加到enemyHatred 并且将当前位置存储为目标点
            self._target.pos[1] = _pos
        else    --如果否则发呆
        end 
    end

    if wantMove then self:CountMoveTo(wantMove,dt,false) 
    else self:SaveMove(0,0) --self:CountMoveTo(_pos,dt,true) 
    end

--    print("我是："..self.id..
--    " 攻击目标为："..(self:enemyAttack()==nil and "无" or self:enemyAttack().id  )
--    .." 仇恨目标为："..(self:enemyHatred()==nil and "无" or self:enemyHatred().id  ) )
--    print("当前移动速度 x="..self.shift.x.." y="..self.shift.y)

    
    --移动
    local pow = {x=0,y=0}
    for i, var in ipairs(self.pow[self.pow.max]) do
        pow = cc.pAdd(pow,var)
    end
    local shift = cc.pMul(pow,1/self:weight())
--    dump(self.pow)
    self:ClearPow()    
    if cc.pGetLength(shift) > cc.pGetLength(self.shift)*0.08 then 
        self:MoveTo(cc.pAdd(_pos,shift))
    end
end

function ActorBase:hatred ()    --仇恨距离
    return self._property.hatred
end
function ActorBase:weight()     --质量
    return self._property.weight
end
function ActorBase:radius()     --半径
    return self._property.radius
end
function ActorBase:enemyAttack(data)
    if data == false then self._target.enemyAttack = nil
    elseif data then self._target.enemyAttack = data
    else return self._target.enemyAttack end
end
function ActorBase:enemyHatred(data)
    if data == false then self._target.enemyHatred = nil    --print("删除仇恨目标")
    elseif data then self._target.enemyHatred = data        --print("添加仇恨目标")
    else return self._target.enemyHatred end
end
function ActorBase:SavePow(pow,level)
    level = level or pow.level
    table.insert(self.pow[level],pow)
    if level > self.pow.max then self.pow.max = level end
end
function ActorBase:ClearPow()
    self.pow={{},{},{}}
    self.pow.max=0
end
function ActorBase:SetRepul(repul)
    self.repul = repul
end
function ActorBase:SetRound(round)
    self.round = round
end

function ActorBase:Update(dt)      
    if self._property.career ~= 3 then
        local pos
        if cc.pDistanceSQ(self:pos(),self._target.pos[1]) > 1 then        
            if self._target.pos[1] then 
                pos = self._target.pos[1]
            else
                local Grid = td.SeekNearestFor8(self:pos(), td.CenterNode:pos()  )
                pos = td.GetPosForGrid(Grid)
                self._target.pos[1] = pos
            end        
        elseif cc.pDistanceSQ(self:pos(),self._target.pos[1]) <= 1 then
            self:MoveTo(self._target.pos[1])
            local Grid = td.SeekNearestFor8(self:pos(), td.CenterNode:pos() )
            pos = td.GetPosForGrid(Grid)
            self._target.pos[1] = pos
        end
        self:CountMoveTo(pos,dt)
        self:MoveTo(cc.pAdd(self:pos(),self.shift))
    else
        self._target.pos[1] = td.GetGridForPos(self:pos())
        
    end
    
    td.MapLayer:GetActorsForPos()
end